#include "Interface_Task.h"

extern osEventFlagsId_t xProcessEventGroupHandle;
extern osMessageQueueId_t InterfaceMessageHandle; // 接收上层Process_Task的控制指令

Interface_TypeDef Interface;
uint8_t QR_NUM[8]; // 二维码数组

static void Interface_Init();
static void Get_GM65_Data(void);
static void Get_Interface_Ctrl_Data_From_Up(void);

/* USER CODE BEGIN Header_Interface_Task */
/**
* @brief Function implementing the InterfaceTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Interface_Task */
void Interface_Task(void *argument)
{
  /* USER CODE BEGIN Interface_Task */
	Interface_Init();
	osDelay(1000);
	TickType_t xLastWakeTime;
	const TickType_t xDelay50ms = pdMS_TO_TICKS( 50 );
	xLastWakeTime = xTaskGetTickCount();
  /* Infinite loop */
  for(;;)
  {
		Get_Interface_Ctrl_Data_From_Up(); // 接收上层数据
	  Get_GM65_Data(); // 接收数据
    vTaskDelayUntil( &xLastWakeTime, xDelay50ms );
  }
  /* USER CODE END Interface_Task */
}

/* 人机交互任务初始化 */
static void Interface_Init()
{
	Interface.GM65_Device = GM65_Init(); // 二维码扫描初始化
	// 屏幕串口初始化
}
/* 接收GM65数据 */ 
static void Get_GM65_Data(void)
{
	if (Interface.Mode != SCAN_QR_MODE) return;
	if (Interface.RecvOk_Flag) return;
	Interface.GM65_Device->Recv(Interface.GM65_Device, Interface.Color_Num, 20);
	if (Interface.Color_Num[7] == 13)
	{
		for (int i = 0; i < 8; i++)
		{
			QR_NUM[i] = Interface.Color_Num[i]; // 传递给Process_Task(这里懒了不想用消息邮箱了)
		}
		Interface.RecvOk_Flag = 1;
		Interface.Mode = STATIC_MODE;
		osEventFlagsSet(xProcessEventGroupHandle, INTERFACE_DONE); // 任务完成发送事件组标志位置1
	}
}
/* 接收来自上层Process_Task的信息 */
#define Interface_Send_Flag 0x01000000U
static void Get_Interface_Ctrl_Data_From_Up(void)
{
	uint32_t Flags = 0;
	Interface_Rx_Message_t g_Rx_Interface_Message;
	Flags = osEventFlagsGet(xProcessEventGroupHandle);
	if (Flags & Interface_Send_Flag)
	{
		if (osMessageQueueGet(InterfaceMessageHandle, &g_Rx_Interface_Message, NULL, 0) == osOK) {
			osEventFlagsClear(xProcessEventGroupHandle, Interface_Send_Flag);// 清零事件组标志位
			Interface.Mode = g_Rx_Interface_Message.InterfaceMode;
			}
	}
}

